<?php 
  header("content-type:text/html;charset=utf8");
  //当用户扫描后绑定账户时使用
  $tel = $_POST['tel'];
  $password =md5($_POST['password']);
  $openid = $_POST['openid'];
  if (!$openid) {
    echo "<script>alert('登录失败，请进行重试');location.href='https://pay.hispot.cn/gitscan/h5/login.php'</script>";
    exit;
  }
  $pdo = pdo_connect_new_database();
  $sql = "SELECT * FROM user WHERE tel = '$tel' AND password = '$password'";
  $login_data = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
  if ($login_data) {
    new_data_existence($tel,$openid,$pdo);
    get_new_data_deposit_old_database($tel,$password,$openid,$pdo);
  }else{
    new_data_no_existence($tel,$password,$openid,$pdo);
  }

  //用户登录时新数据库不存在数据
  function new_data_no_existence($tel,$password,$openid,$pdo){
    //查询旧数据库中的登录信息
    $pdo_user_old = pdo_connect_old_user_database();
    $select_old_user_sql = "SELECT * FROM uc_members WHERE username = '$tel'";
    $old_user_data = $pdo_user_old->query($select_old_user_sql)->fetch(PDO::FETCH_ASSOC);

    //判断数据是否存在以及是否正确
    if ($old_user_data && $old_user_data['password'] == md5($password.$old_user_data['salt'])) {
      //获取新数据库中的绑定信息
      $select_binding_sql = "SELECT * FROM binding WHERE openid = '$openid'";
      $new_binding_data = $pdo->query($select_binding_sql)->fetch(PDO::FETCH_ASSOC);

      //将新数据库的绑定数据同步到旧绑定数据库中
      $pdo_binding_old = pdo_connect_old_binidng_database();
      $uid = $old_user_data['uid'];
      $time = date("Y-m-d H:i:s");
      $sn = $new_binding_data['device_sn'];
      $insert_old_binidng_sql = "INSERT INTO weixin_binging(Uid,OpinID,CreateTime,DeviceSelf) VALUES('$uid','$openid','$time','$sn')";
      $pdo_binding_old->exec($insert_old_binidng_sql);

      //然后通过uid以及openid来查询关联数据是否存在
      $select_relation_data_sql = "SELECT * FROM weixin_user WHERE openId = '$openid' AND user = '$uid'";
      $relation_data = $pdo_binding_old->query($select_relation_data_sql)->fetch(PDO::FETCH_ASSOC);
      //如果数据不存在
      if (!$relation_data) {
        //将uid和openid关联存在weixin_user表中
        $insert_old_relation_sql = "INSERT INTO weixin_user(openId,user) VALUES('$openid','$uid')";
        $pdo_binding_old->exec($insert_old_relation_sql); 
      }

      //最后将旧数据库的登录信息同步到新数据库中
      $insert_new_user_sql = "INSERT INTO user(tel,password,openid,`time`) VALUES('$tel','$password','$openid','$time')";
      $pdo->exec($insert_new_user_sql);
      file_get_contents("https://pay.hispot.cn/gitscan/pushLoginInfo.php?openid=".$openid);
      echo "<script>alert('绑定成功');location.href='https://pay.hispot.cn/gitscan/h5/success.php'</script>";
      exit;
    }else{
      //查询不到对应的账户或者密码不匹配，提示登录错误
      echo "<script>alert('账户或密码输入错误，请核对');location.href='https://pay.hispot.cn/gitscan/h5/login.php?openid=".$openid."'</script>";
      exit;

    }
  }

  //用户登录时查询新数据库数据存在
  function new_data_existence($tel,$openid,$pdo){
    $update_sql = "UPDATE user SET openid = '$openid' WHERE tel = '$tel'";
    $res = $pdo->exec($update_sql);
    if (!$res) {
      echo "<script>alert('绑定失败，可能由于网络原因导致，请检查');location.href='https://pay.hispot.cn/gitscan/h5/login.php?openid=".$openid."'</script>";
      exit;
    }
  }

  //获取新数据库里面的数据存放到旧数据库
  function get_new_data_deposit_old_database($tel,$password,$openid,$pdo){
    //将新登录的信息同步到旧数据库中的登录信息表
    $pdo_user_old = pdo_connect_old_user_database();
    $sql = "SELECT * FROM uc_members WHERE username = '$tel'";
    $data = $pdo_user_old->query($sql)->fetch(PDO::FETCH_ASSOC);
    if ($data) {
          // $delete_sql = "DELETE FROM uc_members WHERE username = '$tel'";
          // $pdo_user_old->exec($delete_sql);
          $uid = $data['uid'];
    }else{
          $salt = substr(md5(rand('1','1000')),0,6);
          $old_password = md5($password.$salt);
          $insert_old_user_sql = "INSERT INTO uc_members(username,password,salt,phone) VALUES('$tel','$old_password','$salt','$tel')";
          $pdo_user_old->exec($insert_old_user_sql);
          $uid = $pdo_user_old->lastInsertId();
    }
  
    //获取新数据库中的绑定信息
    $select_binding_sql = "SELECT * FROM binding WHERE openid = '$openid' ORDER BY b_id DESC";
    $new_binding_data = $pdo->query($select_binding_sql)->fetch(PDO::FETCH_ASSOC);

    //将新数据的绑定信息同步旧数据库的绑定信息表中
    $pdo_binding_old = pdo_connect_old_binidng_database();
    $sn = $new_binding_data['device_sn'];
    $time = date("Y-m-d H:i:s");
    $select_old_binding_sql = "SELECT * FROM weixin_binging WHERE Uid = '$uid' AND OpinID = '$openid' AND DeviceSelf = '$sn'";
    $old_binding_data = $pdo_binding_old->query($select_old_binding_sql)->fetch(PDO::FETCH_ASSOC);
    if (!$old_binding_data) {
        $insert_old_binidng_sql = "INSERT INTO weixin_binging(Uid,OpinID,CreateTime,DeviceSelf) VALUES('$uid','$openid','$time','$sn')";
        $pdo_binding_old->exec($insert_old_binidng_sql);   
    }

    //最后将uid和openid关联添加weixin_user表中 添加之前先通过uid以及openid来查询关联数据是否已经存在
      $select_relation_data_sql = "SELECT * FROM weixin_user WHERE openId = '$openid' AND user = '$uid'";
      $relation_data = $pdo_binding_old->query($select_relation_data_sql)->fetch(PDO::FETCH_ASSOC);
      //如果数据不存在
      if (!$relation_data) {
        //将uid和openid关联存在weixin_user表中
        $insert_old_relation_sql = "INSERT INTO weixin_user(openId,user) VALUES('$openid','$uid')";
        $pdo_binding_old->exec($insert_old_relation_sql); 
      }
    echo "<script>alert('绑定成功');location.href='https://pay.hispot.cn/gitscan/h5/success.php'</script>";
    exit;
  }

  //pdo连接新数据库
  function pdo_connect_new_database(){
    $pdo_new = new PDO('mysql:host=123.57.16.97;dbname=device_binding','root','zqVnUy6cxsQXbpEZ');
    $pdo_new->exec('set names utf8'); 
    return $pdo_new;
  }

  //pdo连接旧用户登录信息库
  function pdo_connect_old_user_database(){
    $pdo_user_old = new PDO('mysql:host=123.57.16.97;dbname=ucenter','root','zqVnUy6cxsQXbpEZ');
    $pdo_user_old->exec('set names utf8'); 
    return $pdo_user_old;
  }

  //pdo连接旧用户绑定表
  function pdo_connect_old_binidng_database(){
    $pdo_binding_old = new PDO('mysql:host=123.57.16.97;dbname=wx_online_movie','root','zqVnUy6cxsQXbpEZ');
    $pdo_binding_old->exec('set names utf8');
    return $pdo_binding_old;
  }

 ?>
